package com.pvdnc.tapkit.chat.chat.client;

import android.content.Context;
import android.content.Intent;

import com.pvdnc.nsocket.log.SLog;
import com.pvdnc.tapkit.utils.BaseLocalReceiver;
import com.pvdnc.tapkit.utils.LocalBroadcast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class FriendReceiver extends BaseLocalReceiver {
    private static final SLog LOG=new SLog(FriendReceiver.class);

    public static final String ACTION="friend.update";

    public FriendReceiver() {
        super(ACTION);
    }

    public static final String EXTRA_FRIEND_MAP ="friend_map";

    private static class FriendCollection implements Serializable{
        public Map<String,ClientInfo> mFriendMap;
    }

    public static void broadcast(Map<String,ClientInfo> friendMap){
        Intent intent=new Intent();
        intent.setAction(ACTION);
        FriendCollection collection=new FriendCollection();
        collection.mFriendMap=friendMap;
        intent.putExtra(EXTRA_FRIEND_MAP,collection);
        LocalBroadcast.getInstance().send(intent);
    }

    private Map<String,ClientInfo> getList(Intent intent){
        Serializable serializable=intent.getSerializableExtra(EXTRA_FRIEND_MAP);
        if(!(serializable instanceof FriendCollection)){
            LOG.e("invalid FriendCollection object");
            return new HashMap<>();
        }
        FriendCollection collection=(FriendCollection)serializable;
        return collection.mFriendMap;
    }

    @Override
    public final void onReceive(Context context, Intent intent) {
        onReceive(context,getList(intent));
    }

    public abstract void onReceive(Context context,Map<String,ClientInfo> friendMap);
}
